package tpdisenio2009.mvp.display;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JOptionPane;
import tpdisenio2009.model.beans.FantasyTeamPlayer;

/**
 *
 * @author DeuterioX
 */
public class QueryFantasyTeamViewImpl extends javax.swing.JPanel implements QueryFantasyTeamView {

    /** Creates new form QueryFantasyTeamViewImpl */
    public QueryFantasyTeamViewImpl() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonMakeChanges = new javax.swing.JButton();
        jButtonMakeTransfers = new javax.swing.JButton();
        jButtonViewPointsByDate = new javax.swing.JButton();
        jLabelFantasyTeamName = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableFantasyTeam = new javax.swing.JTable();
        jPanelPoints = new javax.swing.JPanel();
        jLabelTotalPointsLabel = new javax.swing.JLabel();
        jLabelTotalPoints = new javax.swing.JLabel();
        jLabelValuation = new javax.swing.JLabel();
        jLabelValuationLabel = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(566, 305));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(566, 305));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(tpdisenio2009.Tpdisenio2009App.class).getContext().getResourceMap(QueryFantasyTeamViewImpl.class);
        jButtonMakeChanges.setText(resourceMap.getString("jButtonMakeChanges.text")); // NOI18N
        jButtonMakeChanges.setName("jButtonMakeChanges"); // NOI18N

        jButtonMakeTransfers.setText(resourceMap.getString("jButtonMakeTransfers.text")); // NOI18N
        jButtonMakeTransfers.setName("jButtonMakeTransfers"); // NOI18N

        jButtonViewPointsByDate.setText(resourceMap.getString("jButtonViewPointsByDate.text")); // NOI18N
        jButtonViewPointsByDate.setName("jButtonViewPointsByDate"); // NOI18N

        jLabelFantasyTeamName.setFont(resourceMap.getFont("jLabelFantasyTeamName.font")); // NOI18N
        jLabelFantasyTeamName.setText(resourceMap.getString("jLabelFantasyTeamName.text")); // NOI18N
        jLabelFantasyTeamName.setName("jLabelFantasyTeamName"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTableFantasyTeam.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Arquero", null, null, null},
                {"Defensa 1", null, null, null},
                {"Defensa 2", null, null, null},
                {"Defensa 3", null, null, null},
                {"Defensa 4", null, null, null},
                {"Volante 1", null, null, null},
                {"Volante 2", null, null, null},
                {"Volante 3", null, null, null},
                {"Delantero 1", null, null, null},
                {"Delantero 2", null, null, null},
                {"Delantero 3", null, null, null},
                {"Arquero Suplente", null, null, null},
                {"Defensa Suplente", null, null, null},
                {"Volante Suplente", null, null, null},
                {"Delantero Suplente", null, null, null}
            },
            new String [] {
                "Posición", "Jugador", "Club", "Cotización"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableFantasyTeam.setName("jTableFantasyTeam"); // NOI18N
        jTableFantasyTeam.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTableFantasyTeam.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTableFantasyTeam);
        jTableFantasyTeam.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTableFantasyTeam.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTableFantasyTeam.columnModel.title0")); // NOI18N
        jTableFantasyTeam.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTableFantasyTeam.columnModel.title1")); // NOI18N
        jTableFantasyTeam.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTableFantasyTeam.columnModel.title2")); // NOI18N
        jTableFantasyTeam.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("jTableFantasyTeam.columnModel.title3")); // NOI18N

        jPanelPoints.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanelPoints.setMinimumSize(new java.awt.Dimension(86, 92));
        jPanelPoints.setName("jPanelPoints"); // NOI18N
        jPanelPoints.setPreferredSize(new java.awt.Dimension(86, 92));

        jLabelTotalPointsLabel.setText(resourceMap.getString("jLabelTotalTournamentPointLabel.text")); // NOI18N
        jLabelTotalPointsLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabelTotalPointsLabel.setName("jLabelTotalTournamentPointLabel"); // NOI18N

        jLabelTotalPoints.setFont(resourceMap.getFont("jLabelTotalTournamentPoints.font")); // NOI18N
        jLabelTotalPoints.setText(resourceMap.getString("jLabelTotalTournamentPoints.text")); // NOI18N
        jLabelTotalPoints.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabelTotalPoints.setName("jLabelTotalTournamentPoints"); // NOI18N

        javax.swing.GroupLayout jPanelPointsLayout = new javax.swing.GroupLayout(jPanelPoints);
        jPanelPoints.setLayout(jPanelPointsLayout);
        jPanelPointsLayout.setHorizontalGroup(
            jPanelPointsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPointsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelPointsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabelTotalPoints, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelTotalPointsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14))
        );
        jPanelPointsLayout.setVerticalGroup(
            jPanelPointsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelPointsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTotalPointsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                .addGap(12, 12, 12)
                .addComponent(jLabelTotalPoints, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jLabelTotalPointsLabel.getAccessibleContext().setAccessibleName(resourceMap.getString("jLabelTotalTournamentPointLabel.AccessibleContext.accessibleName")); // NOI18N

        jLabelValuation.setFont(resourceMap.getFont("jLabelContization.font")); // NOI18N
        jLabelValuation.setText(resourceMap.getString("jLabelContization.text")); // NOI18N
        jLabelValuation.setName("jLabelContization"); // NOI18N

        jLabelValuationLabel.setText(resourceMap.getString("jLabelCotizationLabel.text")); // NOI18N
        jLabelValuationLabel.setName("jLabelCotizationLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonMakeChanges)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonMakeTransfers)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonViewPointsByDate))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabelFantasyTeamName, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabelValuationLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabelValuation)
                                .addGap(10, 10, 10))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addComponent(jPanelPoints, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelFantasyTeamName)
                    .addComponent(jLabelValuation)
                    .addComponent(jLabelValuationLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelPoints, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonMakeChanges)
                            .addComponent(jButtonMakeTransfers)
                            .addComponent(jButtonViewPointsByDate))))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonMakeChanges;
    private javax.swing.JButton jButtonMakeTransfers;
    private javax.swing.JButton jButtonViewPointsByDate;
    private javax.swing.JLabel jLabelFantasyTeamName;
    private javax.swing.JLabel jLabelTotalPoints;
    private javax.swing.JLabel jLabelTotalPointsLabel;
    private javax.swing.JLabel jLabelValuation;
    private javax.swing.JLabel jLabelValuationLabel;
    private javax.swing.JPanel jPanelPoints;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableFantasyTeam;
    // End of variables declaration//GEN-END:variables

    public void addMakeChangesListener(ActionListener listener) {
        jButtonMakeChanges.addActionListener(listener);
    }

    public void removeMakeChangesListener(ActionListener listener) {
        jButtonMakeChanges.removeActionListener(listener);
    }

    public void addMakeTransfersListener(ActionListener listener) {
        jButtonMakeTransfers.addActionListener(listener);
    }

    public void removeMakeTransfersListener(ActionListener listener) {
        jButtonMakeTransfers.removeActionListener(listener);
    }

    public void addViewPointsByDateListener(ActionListener listener) {
        jButtonViewPointsByDate.addActionListener(listener);
    }

    public void removeViewPointsByDateListener(ActionListener listener) {
        jButtonViewPointsByDate.removeActionListener(listener);
    }

    public void setFantasyTeamName(String fantasyTeamName) {
        jLabelFantasyTeamName.setText(fantasyTeamName);
    }

    public void setValuation(int valuation) {
        jLabelValuation.setText( String.valueOf(valuation) );
    }

    public void setTotalTournamentScore(int totalScore) {
        jLabelTotalPoints.setText( String.valueOf(totalScore) );
    }

    public void loadFantasyTeam(List<FantasyTeamPlayer> team) {
        FantasyTeamPlayerTableModel model = new FantasyTeamPlayerTableModel( team );
        jTableFantasyTeam.setModel( model );
    }

    public FantasyTeamPlayer getPlayerByRole(String role) {
        FantasyTeamPlayerTableModel model = (FantasyTeamPlayerTableModel) jTableFantasyTeam.getModel();
        return model.getPlayerByRole( role );
    }

    public FantasyTeamPlayer getPlayerByRoleString(String role) {
        FantasyTeamPlayerTableModel model = (FantasyTeamPlayerTableModel) jTableFantasyTeam.getModel();
        return model.getPlayerByRoleString( role );
    }

    public FantasyTeamPlayer getSelectedPlayer() {
        FantasyTeamPlayerTableModel model = (FantasyTeamPlayerTableModel) jTableFantasyTeam.getModel();

        int selectedIndex = jTableFantasyTeam.getSelectedRow();
        if (selectedIndex >= 0)
            return model.getFantasyTeamPlayer( selectedIndex );

        return null;
    }

    public void setPlayerByRole(String role, FantasyTeamPlayer player) {
        FantasyTeamPlayerTableModel model = (FantasyTeamPlayerTableModel) jTableFantasyTeam.getModel();
        model.setPlayerByRole( role, player );
    }

    public void showError(String title, String error) {
        JOptionPane.showMessageDialog(this, error, title, JOptionPane.ERROR_MESSAGE);
    }

    public Component asWidget() {
        return this;
    }

}
